**** Replicate tables for parents with RI p values 

* 0. Read in data 

use "${dataanalysis}\ParentsReplication.dta", clear

* 1. globals
rename (mangroves_0 manclimate manbiodiv manconsume mangroves_6_1_index) (y1 y2 y3 y4 y5) // knowledge
rename (env_att_2 env_att_3 env_att_5_zero env_att_7_zero env_att_11_pos env_att_8_pos env_concern_index2z) (a1 a2 a3 a4 a5 a6 a7) // general environmental attitudes
rename (mangroves_3 mangroves_15 mangroveproj_rank mangroves_1_neg mangroves_1_nat mangroves_1_tour) (m1 m2 m3 m4 m5 m6) // mangrove attitudes
rename  (behavior_2_pos  behavior_3  behavior_4 behavior_6_pos behavior_z) (b1 b2 b3 b4 b5) // behaviors


global envknowledge y1 y2 y3 y4 y5  
global envatt a1 a2 a3 a4 a5 a6 a7
global manatt m1 m2 m3 m4 m5 m6
global envbehav b1 b2 b3 b4 b5 
global summarytable y5 a7 m3 b5 

* 2. preliminaries
set scheme tab1
* recode so that positive indicates positive attitude rather than absence of negative attitude
recode m1 m2 (0=1) (1=0)

* generate interactions for randcmd, which does not accept factors
g treatrisk = envrisk_zscore*treatindiv
g treattime = timesincetreatment*treatindiv
g treatgender = dem_gender*treatindiv

drop if parentattrite == 1
drop if nobaseline == 1
xtset id_p 


* 3. knowledge table
if 0{
estimates clear


preserve
gen yn = 0 // generate another variable as a placement column to add joint p-value for now. 
la var yn "Joint test"


drop if spillsample == 1


/*

RI: average treatment


*/
randcmd ///
((treatindiv) eststo xtr1: xtreg y1 treatindiv post, fe nonest vce(cluster club_id)) ///
((treatindiv) eststo xtr2: xtreg y2 treatindiv post, fe nonest vce(cluster club_id)) ///
((treatindiv) eststo xtr3: xtreg y3 treatindiv post, fe nonest vce(cluster club_id)) ///
((treatindiv) eststo xtr4: xtreg y4 treatindiv post, fe nonest vce(cluster club_id)) ///
((treatindiv) eststo xtr5: xtreg y5 treatindiv post, fe nonest vce(cluster club_id)) ///
, treatvars(treatindividual) groupvar(club_id)  seed(1234) reps(1000) calc1(replace treatindiv = treatindividual*post)

matrix list e(RCoef) //  Randomization-c and -t p-values for individual coefficients.
matrix list e(RMult) //  Randomization-c and -t p-values for the Westfall-Young multiple testing 

// rand t p-value for each column
matrix rcoef = e(RCoef)

// WY p-value for last column 
matrix rmult = e(RMult)

matrix rivals = J(6,1,.)
forvalues i = 1/5 {
		matrix rivals[`i',1] = rcoef[`i',6]
}

matrix rivals[6,1] = rmult[6,6]
local wy = rmult[6,6]
mat colnames rivals = treatindiv 
mat rownames rivals = y1 y2 y3 y4 y5 yn


file open newfile  using "$tables/pwyknowledge.tex", write replace 
local wy: di %4.3f `wy'
file write newfile ("`wy'") 
file close newfile


foreach var in $envknowledge yn {
  eststo xtr_`var': xtreg `var' treatindiv post, fe nonest vce(cluster club_id)
   estadd matrix ri = rivals["`var'",1...]
}

/*

RI: treatment and time


*/
	
randcmd ///
((treatindiv treattime) eststo xtr1: xtreg y1 treatindiv treattime post, fe nonest vce(cluster club_id)) ///
((treatindiv treattime) eststo xtr2: xtreg y2 treatindiv treattime post, fe nonest vce(cluster club_id)) ///
((treatindiv treattime) eststo xtr3: xtreg y3 treatindiv treattime post, fe nonest vce(cluster club_id)) ///
((treatindiv treattime) eststo xtr4: xtreg y4 treatindiv treattime post, fe nonest vce(cluster club_id)) ///
((treatindiv treattime) eststo xtr5: xtreg y5 treatindiv treattime post, fe nonest vce(cluster club_id)) ///
, treatvars(treatindividual) groupvar(club_id)  seed(1234) reps(1000) calc1(replace treatindiv = treatindividual*post) calc2(replace treattime = timesincetreatment*treatindividual*post)	
		

matrix list e(RCoef) //  Randomization-c and -t p-values for individual coefficients.
matrix list e(RMult) //  Randomization-c and -t p-values for the Westfall-Young multiple testing 

// rand t p-value for each column
matrix rcoef = e(RCoef)

// WY p-value for last column 
matrix rmult = e(RMult)

matrix rivals = J(6,2,.)
forvalues i = 1/5 {
		scalar odd = 2*`i' - 1   // odd placement across rows for treat
		local j = odd
		matrix rivals[`i',1] = rcoef[`j',6]
		scalar even = 2*`i'  // even placement across rows for treat*time
		local k = even
		matrix rivals[`i',2] = rcoef[`k',6]
}

matrix rivals[6,2] = rmult[6,6]

mat colnames rivals = treatindiv treattime
mat rownames rivals = y1 y2 y3 y4 y5 yn
matrix list rivals

foreach var in $envknowledge yn  {
  eststo xtrtime1_`var': xtreg `var' treatindiv post treattime, fe nonest vce(cluster club_id)
  estadd matrix ri = rivals["`var'",1...]	
  
  } 
  
/*

RI:  treatment and gender


*/ 
	
randcmd ///
((treatindiv treatgender) eststo xtr1: xtreg y1 treatindiv treatgender post, fe nonest vce(cluster club_id)) ///
((treatindiv treatgender) eststo xtr2: xtreg y2 treatindiv treatgender post, fe nonest vce(cluster club_id)) ///
((treatindiv treatgender) eststo xtr3: xtreg y3 treatindiv treatgender post, fe nonest vce(cluster club_id)) ///
((treatindiv treatgender) eststo xtr4: xtreg y4 treatindiv treatgender post, fe nonest vce(cluster club_id)) ///
((treatindiv treatgender) eststo xtr5: xtreg y5 treatindiv treatgender post, fe nonest vce(cluster club_id)) ///
, treatvars(treatindividual) groupvar(club_id)  seed(1234) reps(1000) calc1(replace treatindiv = treatindividual*post) calc2(replace treatgender = dem_gender*treatindividual*post)	
		

matrix list e(RCoef) //  Randomization-c and -t p-values for individual coefficients.
matrix list e(RMult) //  Randomization-c and -t p-values for the Westfall-Young multiple testing 

// rand t p-value for each column
matrix rcoef = e(RCoef)

// WY p-value for last column 
matrix rmult = e(RMult)

matrix rivals = J(6,2,.)
forvalues i = 1/5 {
		scalar odd = 2*`i' - 1   // odd placement across rows for treat
		local j = odd
		matrix rivals[`i',1] = rcoef[`j',6]
		scalar even = 2*`i'  // even placement across rows for treat*time
		local k = even
		matrix rivals[`i',2] = rcoef[`k',6]
}

matrix rivals[6,2] = rmult[6,6]

mat colnames rivals = treatindiv treatgender
mat rownames rivals = y1 y2 y3 y4 y5 yn
matrix list rivals
 
  
  

foreach var in $envknowledge yn {
  eststo xtrgen1_`var': xtreg `var' treatindiv post treatgender, fe nonest vce(cluster club_id)
  estadd matrix ri = rivals["`var'",1...]	
	}

	
/*

RI:  treatment and environmental risk


*/
randcmd ///
((treatindiv treatrisk) eststo xtr1: xtreg y1 treatindiv treatrisk post, fe nonest vce(cluster club_id)) ///
((treatindiv treatrisk) eststo xtr2: xtreg y2 treatindiv treatrisk post, fe nonest vce(cluster club_id)) ///
((treatindiv treatrisk) eststo xtr3: xtreg y3 treatindiv treatrisk post, fe nonest vce(cluster club_id)) ///
((treatindiv treatrisk) eststo xtr4: xtreg y4 treatindiv treatrisk post, fe nonest vce(cluster club_id)) ///
((treatindiv treatrisk) eststo xtr5: xtreg y5 treatindiv treatrisk post, fe nonest vce(cluster club_id)) ///
, treatvars(treatindividual) groupvar(club_id)  seed(1234) reps(1000) calc1(replace treatindiv = treatindividual*post) calc2(replace treatrisk = envrisk_zscore*treatindividual*post)	
		

matrix list e(RCoef) //  Randomization-c and -t p-values for individual coefficients.
matrix list e(RMult) //  Randomization-c and -t p-values for the Westfall-Young multiple testing 

// rand t p-value for each column
matrix rcoef = e(RCoef)

// WY p-value for last column 
matrix rmult = e(RMult)

matrix rivals = J(6,2,.)
forvalues i = 1/5 {
		scalar odd = 2*`i' - 1   // odd placement across rows for treat
		local j = odd
		matrix rivals[`i',1] = rcoef[`j',6]
		scalar even = 2*`i'  // even placement across rows for treat*time
		local k = even
		matrix rivals[`i',2] = rcoef[`k',6]
		
}

matrix rivals[6,2] = rmult[6,6]

mat colnames rivals = treatindiv treatrisk
mat rownames rivals = y1 y2 y3 y4 y5 yn 	

foreach var in $envknowledge yn {
   
  eststo xtrrisk1_`var': xtreg `var' treatindiv post treatrisk envrisk_zscore, fe nonest vce(cluster club_id)
  estadd matrix ri = rivals["`var'",1...]
  sum `var' if dem_gender == 0 & treatment == 1 & post == 0
  local ymeanboys = round(r(mean),0.01)
  estadd scalar sb `ymeanboys':  xtrrisk1_`var'
  sum `var' if  dem_gender == 1 & treatment == 1 & post == 0
  local ymeangirls = round(r(mean),0.01)
  estadd scalar sg `ymeangirls':  xtrrisk1_`var'
  sum `var'
  local obs = r(N) 	
  estadd scalar o1 `obs': xtrrisk1_`var'
}
 
restore




		
esttab xtr_y1 xtr_y2 xtr_y3 xtr_y4 xtr_y5 using "$tables/parentsknowledge.tex", replace se nonotes noobs style(tex) ///
cell(b(fmt(a3) star pvalue(ri)) se(fmt(a3) par) ri(fmt(a3) par([ ])))  ///
keep(treatindiv) nolines ///
coeflabels(treatindiv "Treated") nolines collabels(none) ///
		starlevels(* 0.10 ** 0.05 *** 0.01) label nomtitles ///
		nonumbers  fragment  ///
		prehead( ) ///
		posthead(\\ \midrule \\) ///
		prefoot() ///
		postfoot()
		
esttab xtrgen1_y1 xtrgen1_y2 xtrgen1_y3 xtrgen1_y4 xtrgen1_y5 using "$tables/parentsknowledge.tex", append se nonotes style(tex) noobs b(%12.3f) se(%12.3f) ///
keep(treatindiv treatgender) nolines  collabels(none) ///
cell(b(fmt(a3) star pvalue(ri)) se(fmt(a3) par) ri(fmt(a3) par([ ])))  ///
coeflabels(treatindiv "Treated" treatgender "Treated $\times$ Female") ///
		starlevels(* 0.10 ** 0.05 *** 0.01) nomtitles ///
		nonumbers  fragment  ///
		prehead( \\ ) ///
		posthead(\midrule \\) ///
		prefoot() ///
		postfoot()
		
esttab xtrtime1_y1 xtrtime1_y2 xtrtime1_y3 xtrtime1_y4 xtrtime1_y5 using "$tables/parentsknowledge.tex", append se nonotes style(tex) noobs b(%12.3f) se(%12.3f) ///
keep(treatindiv treattime) nolines collabels(none)  ///
cell(b(fmt(a3) star pvalue(ri)) se(fmt(a3) par) ri(fmt(a3) par([ ])))  ///
coeflabels(treatindiv "Treated" treattime "Treated $\times$ Time since treated") ///
		starlevels(* 0.10 ** 0.05 *** 0.01) nomtitles ///
		nonumbers  fragment  ///
		prehead( \\  ) ///
		posthead(\midrule \\) ///
		prefoot() ///
		postfoot()
		
esttab xtrrisk1_y1 xtrrisk1_y2 xtrrisk1_y3 xtrrisk1_y4 xtrrisk1_y5 using "$tables/parentsknowledge.tex", append se nonotes style(tex) noobs scalar("sb Mean control men" "sg Mean control women" "o1 Observations") sfmt(%12.3f %12.3f %12.0f) b(%12.3f) se(%12.3f) ///
keep(treatindiv treatrisk) nolines collabels(none) ///
cell(b(fmt(a3) star pvalue(ri)) se(fmt(a3) par) ri(fmt(a3) par([ ])))  ///
coeflabels(treatindiv "Treated" treatrisk "Treated $\times$ environmental risk") ///
		starlevels(* 0.10 ** 0.05 *** 0.01) nomtitles ///
		nonumbers  fragment  ///
		prehead( \\ ) ///
		posthead(\midrule \\) ///
		prefoot( \\ \midrule) ///
		postfoot()

}

		
* 4. Env attitudes table
estimates clear 

if 0{
	
estimates clear


preserve
gen an = 0 // generate another variable as a placement column to add joint p-value for now. 
la var an "Joint test"


drop if spillsample == 1


/*

RI: average treatment


*/
randcmd ///
((treatindiv) eststo xtr1: xtreg a1 treatindiv post, fe nonest vce(cluster club_id)) ///
((treatindiv) eststo xtr2: xtreg a2 treatindiv post, fe nonest vce(cluster club_id)) ///
((treatindiv) eststo xtr3: xtreg a3 treatindiv post, fe nonest vce(cluster club_id)) ///
((treatindiv) eststo xtr4: xtreg a4 treatindiv post, fe nonest vce(cluster club_id)) ///
((treatindiv) eststo xtr5: xtreg a5 treatindiv post, fe nonest vce(cluster club_id)) ///
((treatindiv) eststo xtr6: xtreg a6 treatindiv post, fe nonest vce(cluster club_id)) ///
((treatindiv) eststo xtr7: xtreg a7 treatindiv post, fe nonest vce(cluster club_id)) ///
, treatvars(treatindividual) groupvar(club_id)  seed(1234) reps(1000) calc1(replace treatindiv = treatindividual*post)

matrix list e(RCoef) //  Randomization-c and -t p-values for individual coefficients.
matrix list e(RMult) //  Randomization-c and -t p-values for the Westfall-Young multiple testing 

// rand t p-value for each column
matrix rcoef = e(RCoef)

// WY p-value for last column 
matrix rmult = e(RMult)

matrix rivals = J(8,1,.)
forvalues i = 1/7 {
		matrix rivals[`i',1] = rcoef[`i',6]
}

matrix rivals[8,1] = rmult[8,6]
local wy = rmult[8,6]
mat colnames rivals = treatindiv 
mat rownames rivals = a1 a2 a3 a4 a5 a6 a7 an

file open newfile  using "$tables/pwyatt.tex", write replace 
local wy: di %4.3f `wy'
file write newfile ("`wy'") 
file close newfile



foreach var in $envatt an {
  eststo xtr_`var': xtreg `var' treatindiv post, fe nonest vce(cluster club_id)
   estadd matrix ri = rivals["`var'",1...]
}

/*

RI: treatment and time


*/
	
randcmd ///
((treatindiv treattime) eststo xtr1: xtreg a1 treatindiv treattime post, fe nonest vce(cluster club_id)) ///
((treatindiv treattime) eststo xtr2: xtreg a2 treatindiv treattime post, fe nonest vce(cluster club_id)) ///
((treatindiv treattime) eststo xtr3: xtreg a3 treatindiv treattime post, fe nonest vce(cluster club_id)) ///
((treatindiv treattime) eststo xtr4: xtreg a4 treatindiv treattime post, fe nonest vce(cluster club_id)) ///
((treatindiv treattime) eststo xtr5: xtreg a5 treatindiv treattime post, fe nonest vce(cluster club_id)) ///
((treatindiv treattime) eststo xtr6: xtreg a6 treatindiv treattime post, fe nonest vce(cluster club_id)) ///
((treatindiv treattime) eststo xtr7: xtreg a7 treatindiv treattime post, fe nonest vce(cluster club_id)) ///
, treatvars(treatindividual) groupvar(club_id)  seed(1234) reps(1000) calc1(replace treatindiv = treatindividual*post) calc2(replace treattime = timesincetreatment*treatindividual*post)	
		

matrix list e(RCoef) //  Randomization-c and -t p-values for individual coefficients.
matrix list e(RMult) //  Randomization-c and -t p-values for the Westfall-Young multiple testing 

// rand t p-value for each column
matrix rcoef = e(RCoef)

// WY p-value for last column 
matrix rmult = e(RMult)

matrix rivals = J(8,2,.)
forvalues i = 1/7 {
		scalar odd = 2*`i' - 1   // odd placement across rows for treat
		local j = odd
		matrix rivals[`i',1] = rcoef[`j',6]
		scalar even = 2*`i'  // even placement across rows for treat*time
		local k = even
		matrix rivals[`i',2] = rcoef[`k',6]
}

matrix rivals[8,2] = rmult[8,6]

mat colnames rivals = treatindiv treattime
mat rownames rivals =a1 a2 a3 a4 a5 a6 a7 an
matrix list rivals

foreach var in $envatt an {
  eststo xtrtime1_`var': xtreg `var' treatindiv post treattime, fe nonest vce(cluster club_id)
  estadd matrix ri = rivals["`var'",1...]	
  
  } 
  
/*

RI:  treatment and gender


*/ 
	
randcmd ///
((treatindiv treatgender) eststo xtr1: xtreg a1 treatindiv treatgender post, fe nonest vce(cluster club_id)) ///
((treatindiv treatgender) eststo xtr2: xtreg a2 treatindiv treatgender post, fe nonest vce(cluster club_id)) ///
((treatindiv treatgender) eststo xtr3: xtreg a3 treatindiv treatgender post, fe nonest vce(cluster club_id)) ///
((treatindiv treatgender) eststo xtr4: xtreg a4 treatindiv treatgender post, fe nonest vce(cluster club_id)) ///
((treatindiv treatgender) eststo xtr5: xtreg a5 treatindiv treatgender post, fe nonest vce(cluster club_id)) ///
((treatindiv treatgender) eststo xtr6: xtreg a6 treatindiv treatgender post, fe nonest vce(cluster club_id)) ///
((treatindiv treatgender) eststo xtr7: xtreg a7 treatindiv treatgender post, fe nonest vce(cluster club_id)) ///
, treatvars(treatindividual) groupvar(club_id)  seed(1234) reps(1000) calc1(replace treatindiv = treatindividual*post) calc2(replace treatgender = dem_gender*treatindividual*post)	
		

matrix list e(RCoef) //  Randomization-c and -t p-values for individual coefficients.
matrix list e(RMult) //  Randomization-c and -t p-values for the Westfall-Young multiple testing 

// rand t p-value for each column
matrix rcoef = e(RCoef)

// WY p-value for last column 
matrix rmult = e(RMult)

matrix rivals = J(8,2,.)
forvalues i = 1/7 {
		scalar odd = 2*`i' - 1   // odd placement across rows for treat
		local j = odd
		matrix rivals[`i',1] = rcoef[`j',6]
		scalar even = 2*`i'  // even placement across rows for treat*time
		local k = even
		matrix rivals[`i',2] = rcoef[`k',6]
}

matrix rivals[8,2] = rmult[8,6]

mat colnames rivals = treatindiv treatgender
mat rownames rivals = a1 a2 a3 a4 a5 a6 a7 an
matrix list rivals
  

foreach var in $envatt an {
  eststo xtrgen1_`var': xtreg `var' treatindiv post treatgender, fe nonest vce(cluster club_id)
  estadd matrix ri = rivals["`var'",1...]	
	}

	
/*

RI:  treatment and environmental risk


*/
randcmd ///
((treatindiv treatrisk) eststo xtr1: xtreg a1 treatindiv treatrisk post, fe nonest vce(cluster club_id)) ///
((treatindiv treatrisk) eststo xtr2: xtreg a2 treatindiv treatrisk post, fe nonest vce(cluster club_id)) ///
((treatindiv treatrisk) eststo xtr3: xtreg a3 treatindiv treatrisk post, fe nonest vce(cluster club_id)) ///
((treatindiv treatrisk) eststo xtr4: xtreg a4 treatindiv treatrisk post, fe nonest vce(cluster club_id)) ///
((treatindiv treatrisk) eststo xtr5: xtreg a5 treatindiv treatrisk post, fe nonest vce(cluster club_id)) ///
((treatindiv treatrisk) eststo xtr6: xtreg a6 treatindiv treatrisk post, fe nonest vce(cluster club_id)) ///
((treatindiv treatrisk) eststo xtr7: xtreg a7 treatindiv treatrisk post, fe nonest vce(cluster club_id)) ///
, treatvars(treatindividual) groupvar(club_id)   seed(1234) reps(1000) calc1(replace treatindiv = treatindividual*post) calc2(replace treatrisk = envrisk_zscore*treatindividual*post)	
		

matrix list e(RCoef) //  Randomization-c and -t p-values for individual coefficients.
matrix list e(RMult) //  Randomization-c and -t p-values for the Westfall-Young multiple testing 

// rand t p-value for each column
matrix rcoef = e(RCoef)

// WY p-value for last column 
matrix rmult = e(RMult)

matrix rivals = J(8,2,.)
forvalues i = 1/7 {
		scalar odd = 2*`i' - 1   // odd placement across rows for treat
		local j = odd
		matrix rivals[`i',1] = rcoef[`j',6]
		scalar even = 2*`i'  // even placement across rows for treat*time
		local k = even
		matrix rivals[`i',2] = rcoef[`k',6]
		
}

matrix rivals[8,2] = rmult[8,6]

mat colnames rivals = treatindiv treatrisk
mat rownames rivals  = a1 a2 a3 a4 a5 a6 a7 an

foreach var in $envatt an {
   
  eststo xtrrisk1_`var': xtreg `var' treatindiv post treatrisk envrisk_zscore, fe nonest vce(cluster club_id)
  estadd matrix ri = rivals["`var'",1...]
  sum `var' if dem_gender == 0 & treatment == 1 & post == 0
  local ymeanboys = round(r(mean),0.01)
  estadd scalar sb `ymeanboys':  xtrrisk1_`var'
  sum `var' if dem_gender == 1 & treatment == 1 & post == 0
  local ymeangirls = round(r(mean),0.01)
  estadd scalar sg `ymeangirls':  xtrrisk1_`var'
  sum `var'
  local obs = r(N) 	
  estadd scalar o1 `obs': xtrrisk1_`var'
}
 
restore




		
esttab xtr_a1 xtr_a2 xtr_a3 xtr_a4 xtr_a5 xtr_a6 xtr_a7 using "$tables/parentsattitude.tex", replace se nonotes noobs style(tex) ///
cell(b(fmt(a3) star pvalue(ri)) se(fmt(a3) par) ri(fmt(a3) par([ ])))  ///
keep(treatindiv) nolines ///
coeflabels(treatindiv "Treated") nolines collabels(none) ///
		starlevels(* 0.10 ** 0.05 *** 0.01) label nomtitles ///
		nonumbers  fragment  ///
		prehead( ) ///
		posthead(\\ \midrule \\) ///
		prefoot() ///
		postfoot()
		
esttab xtrgen1_a1 xtrgen1_a2 xtrgen1_a3 xtrgen1_a4 xtrgen1_a5 xtrgen1_a6 xtrgen1_a7 using "$tables/parentsattitude.tex", append se nonotes style(tex) noobs b(%12.3f) se(%12.3f) ///
keep(treatindiv treatgender) nolines  collabels(none) ///
cell(b(fmt(a3) star pvalue(ri)) se(fmt(a3) par) ri(fmt(a3) par([ ])))  ///
coeflabels(treatindiv "Treated" treatgender "Treated $\times$ Female") ///
		starlevels(* 0.10 ** 0.05 *** 0.01) nomtitles ///
		nonumbers  fragment  ///
		prehead( \\ ) ///
		posthead(\midrule \\) ///
		prefoot() ///
		postfoot()
		
esttab xtrtime1_a1 xtrtime1_a2 xtrtime1_a3 xtrtime1_a4 xtrtime1_a5 xtrtime1_a6 xtrtime1_a7 using "$tables/parentsattitude.tex", append se nonotes style(tex) noobs b(%12.3f) se(%12.3f) ///
keep(treatindiv treattime) nolines collabels(none)  ///
cell(b(fmt(a3) star pvalue(ri)) se(fmt(a3) par) ri(fmt(a3) par([ ])))  ///
coeflabels(treatindiv "Treated" treattime "Treated $\times$ Time since treated") ///
		starlevels(* 0.10 ** 0.05 *** 0.01) nomtitles ///
		nonumbers  fragment  ///
		prehead( \\  ) ///
		posthead(\midrule \\) ///
		prefoot() ///
		postfoot()
		
esttab xtrrisk1_a1 xtrrisk1_a2 xtrrisk1_a3 xtrrisk1_a4 xtrrisk1_a5 xtrrisk1_a6 xtrrisk1_a7 using "$tables/parentsattitude.tex", append se nonotes style(tex) noobs scalar("sb Mean control men" "sg Mean control women" "o1 Observations") sfmt(%12.3f %12.3f %12.0f) b(%12.3f) se(%12.3f) ///
keep(treatindiv treatrisk) nolines collabels(none) ///
cell(b(fmt(a3) star pvalue(ri)) se(fmt(a3) par) ri(fmt(a3) par([ ])))  ///
coeflabels(treatindiv "Treated" treatrisk "Treated $\times$ environmental risk") ///
		starlevels(* 0.10 ** 0.05 *** 0.01) nomtitles ///
		nonumbers  fragment  ///
		prehead( \\ ) ///
		posthead(\midrule \\) ///
		prefoot( \\ \midrule) ///
		postfoot()	
	
}

* 5. Mangrove attitudes table

if 0{
	
estimates clear


preserve
gen mn = 0 // generate another variable as a placement column to add joint p-value for now. 
la var mn "Joint test"


drop if spillsample == 1


/*

RI: average treatment


*/
randcmd ///
((treatindiv) eststo xtr1: xtreg m1 treatindiv post, fe nonest vce(cluster club_id)) ///
((treatindiv) eststo xtr2: xtreg m2 treatindiv post, fe nonest vce(cluster club_id)) ///
((treatindiv) eststo xtr3: xtreg m3 treatindiv post, fe nonest vce(cluster club_id)) ///
((treatindiv) eststo xtr4: xtreg m4 treatindiv post, fe nonest vce(cluster club_id)) ///
((treatindiv) eststo xtr5: xtreg m5 treatindiv post, fe nonest vce(cluster club_id)) ///
((treatindiv) eststo xtr6: xtreg m6 treatindiv post, fe nonest vce(cluster club_id)) ///
, treatvars(treatindividual) groupvar(club_id)  seed(1234) reps(1000) calc1(replace treatindiv = treatindividual*post)

matrix list e(RCoef) //  Randomization-c and -t p-values for individual coefficients.
matrix list e(RMult) //  Randomization-c and -t p-values for the Westfall-Young multiple testing 

// rand t p-value for each column
matrix rcoef = e(RCoef)

// WY p-value for last column 
matrix rmult = e(RMult)

matrix rivals = J(7,1,.)
forvalues i = 1/6 {
		matrix rivals[`i',1] = rcoef[`i',6]
}

matrix rivals[7,1] = rmult[7,6]
local wy = rmult[7,6]
mat colnames rivals = treatindiv 
mat rownames rivals = m1 m2 m3 m4 m5 m6 mn

file open newfile  using "$tables/pwymatt.tex", write replace 
local wy: di %4.3f `wy'
file write newfile ("`wy'") 
file close newfile

foreach var in $manatt mn {
  eststo xtr_`var': xtreg `var' treatindiv post, fe nonest vce(cluster club_id)
   estadd matrix ri = rivals["`var'",1...]
}

/*

RI: treatment and time


*/
	
randcmd ///
((treatindiv treattime) eststo xtr1: xtreg m1 treatindiv treattime post, fe nonest vce(cluster club_id)) ///
((treatindiv treattime) eststo xtr2: xtreg m2 treatindiv treattime post, fe nonest vce(cluster club_id)) ///
((treatindiv treattime) eststo xtr3: xtreg m3 treatindiv treattime post, fe nonest vce(cluster club_id)) ///
((treatindiv treattime) eststo xtr4: xtreg m4 treatindiv treattime post, fe nonest vce(cluster club_id)) ///
((treatindiv treattime) eststo xtr5: xtreg m5 treatindiv treattime post, fe nonest vce(cluster club_id)) ///
((treatindiv treattime) eststo xtr6: xtreg m6 treatindiv treattime post, fe nonest vce(cluster club_id)) ///
, treatvars(treatindividual) groupvar(club_id)  seed(1234) reps(1000) calc1(replace treatindiv = treatindividual*post) calc2(replace treattime = timesincetreatment*treatindividual*post)	
		

matrix list e(RCoef) //  Randomization-c and -t p-values for individual coefficients.
matrix list e(RMult) //  Randomization-c and -t p-values for the Westfall-Young multiple testing 

// rand t p-value for each column
matrix rcoef = e(RCoef)

// WY p-value for last column 
matrix rmult = e(RMult)

matrix rivals = J(7,2,.)
forvalues i = 1/6 {
		scalar odd = 2*`i' - 1   // odd placement across rows for treat
		local j = odd
		matrix rivals[`i',1] = rcoef[`j',6]
		scalar even = 2*`i'  // even placement across rows for treat*time
		local k = even
		matrix rivals[`i',2] = rcoef[`k',6]
}

matrix rivals[7,2] = rmult[7,6]

mat colnames rivals = treatindiv treattime
mat rownames rivals = m1 m2 m3 m4 m5 m6 mn
matrix list rivals

foreach var in $manatt mn {
  eststo xtrtime1_`var': xtreg `var' treatindiv post treattime, fe nonest vce(cluster club_id)
  estadd matrix ri = rivals["`var'",1...]	
  
  } 
  
/*

RI:  treatment and gender


*/ 
	
randcmd ///
((treatindiv treatgender) eststo xtr1: xtreg m1 treatindiv treatgender post, fe nonest vce(cluster club_id)) ///
((treatindiv treatgender) eststo xtr2: xtreg m2 treatindiv treatgender post, fe nonest vce(cluster club_id)) ///
((treatindiv treatgender) eststo xtr3: xtreg m3 treatindiv treatgender post, fe nonest vce(cluster club_id)) ///
((treatindiv treatgender) eststo xtr4: xtreg m4 treatindiv treatgender post, fe nonest vce(cluster club_id)) ///
((treatindiv treatgender) eststo xtr5: xtreg m5 treatindiv treatgender post, fe nonest vce(cluster club_id)) ///
((treatindiv treatgender) eststo xtr6: xtreg m6 treatindiv treatgender post, fe nonest vce(cluster club_id)) ///
, treatvars(treatindividual) groupvar(club_id)   seed(1234) reps(1000) calc1(replace treatindiv = treatindividual*post) calc2(replace treatgender = dem_gender*treatindividual*post)	
		

matrix list e(RCoef) //  Randomization-c and -t p-values for individual coefficients.
matrix list e(RMult) //  Randomization-c and -t p-values for the Westfall-Young multiple testing 

// rand t p-value for each column
matrix rcoef = e(RCoef)

// WY p-value for last column 
matrix rmult = e(RMult)

matrix rivals = J(7,2,.)
forvalues i = 1/6 {
		scalar odd = 2*`i' - 1   // odd placement across rows for treat
		local j = odd
		matrix rivals[`i',1] = rcoef[`j',6]
		scalar even = 2*`i'  // even placement across rows for treat*time
		local k = even
		matrix rivals[`i',2] = rcoef[`k',6]
}

matrix rivals[7,2] = rmult[7,6]

mat colnames rivals = treatindiv treatgender
mat rownames rivals = m1 m2 m3 m4 m5 m6 mn
matrix list rivals
  

foreach var in $manatt mn {
  eststo xtrgen1_`var': xtreg `var' treatindiv post treatgender, fe nonest vce(cluster club_id)
  estadd matrix ri = rivals["`var'",1...]	
	}

	
/*

RI:  treatment and environmental risk


*/
randcmd ///
((treatindiv treatrisk) eststo xtr1: xtreg m1 treatindiv treatrisk post, fe nonest vce(cluster club_id)) ///
((treatindiv treatrisk) eststo xtr2: xtreg m2 treatindiv treatrisk post, fe nonest vce(cluster club_id)) ///
((treatindiv treatrisk) eststo xtr3: xtreg m3 treatindiv treatrisk post, fe nonest vce(cluster club_id)) ///
((treatindiv treatrisk) eststo xtr4: xtreg m4 treatindiv treatrisk post, fe nonest vce(cluster club_id)) ///
((treatindiv treatrisk) eststo xtr5: xtreg m5 treatindiv treatrisk post, fe nonest vce(cluster club_id)) ///
((treatindiv treatrisk) eststo xtr6: xtreg m6 treatindiv treatrisk post, fe nonest vce(cluster club_id)) ///
, treatvars(treatindividual) groupvar(club_id)   seed(1234) reps(1000) calc1(replace treatindiv = treatindividual*post) calc2(replace treatrisk = envrisk_zscore*treatindividual*post)	
		

matrix list e(RCoef) //  Randomization-c and -t p-values for individual coefficients.
matrix list e(RMult) //  Randomization-c and -t p-values for the Westfall-Young multiple testing 

// rand t p-value for each column
matrix rcoef = e(RCoef)

// WY p-value for last column 
matrix rmult = e(RMult)

matrix rivals = J(7,2,.)
forvalues i = 1/6 {
		scalar odd = 2*`i' - 1   // odd placement across rows for treat
		local j = odd
		matrix rivals[`i',1] = rcoef[`j',6]
		scalar even = 2*`i'  // even placement across rows for treat*time
		local k = even
		matrix rivals[`i',2] = rcoef[`k',6]
		
}

matrix rivals[7,2] = rmult[7,6]

mat colnames rivals = treatindiv treatrisk
mat rownames rivals  = m1 m2 m3 m4 m5 m6 mn

foreach var in $manatt mn {
   
  eststo xtrrisk1_`var': xtreg `var' treatindiv post treatrisk envrisk_zscore, fe nonest vce(cluster club_id)
  estadd matrix ri = rivals["`var'",1...]
  sum `var' if dem_gender == 0 & treatment == 1 & post == 0
  local ymeanboys = round(r(mean),0.01)
  estadd scalar sb `ymeanboys':  xtrrisk1_`var'
  sum `var' if  dem_gender == 1 & treatment == 1 & post == 0
  local ymeangirls = round(r(mean),0.01)
  estadd scalar sg `ymeangirls':  xtrrisk1_`var'
  sum `var'
  local obs = r(N) 	
  estadd scalar o1 `obs': xtrrisk1_`var'
}
 
restore




		
esttab xtr_m1 xtr_m2 xtr_m3 xtr_m4 xtr_m5 xtr_m6  using "$tables/parentsmanatt.tex", replace se nonotes noobs style(tex) ///
cell(b(fmt(a3) star pvalue(ri)) se(fmt(a3) par) ri(fmt(a3) par([ ])))  ///
keep(treatindiv) nolines ///
coeflabels(treatindiv "Treated") nolines collabels(none) ///
		starlevels(* 0.10 ** 0.05 *** 0.01) label nomtitles ///
		nonumbers  fragment  ///
		prehead( ) ///
		posthead(\\ \midrule \\) ///
		prefoot() ///
		postfoot()
		
esttab xtrgen1_m1 xtrgen1_m2 xtrgen1_m3 xtrgen1_m4 xtrgen1_m5 xtrgen1_m6 using "$tables/parentsmanatt.tex", append se nonotes style(tex) noobs b(%12.3f) se(%12.3f) ///
keep(treatindiv treatgender) nolines  collabels(none) ///
cell(b(fmt(a3) star pvalue(ri)) se(fmt(a3) par) ri(fmt(a3) par([ ])))  ///
coeflabels(treatindiv "Treated" treatgender "Treated $\times$ Female") ///
		starlevels(* 0.10 ** 0.05 *** 0.01) nomtitles ///
		nonumbers  fragment  ///
		prehead( \\ ) ///
		posthead(\midrule \\) ///
		prefoot() ///
		postfoot()
		
esttab xtrtime1_m1 xtrtime1_m2 xtrtime1_m3 xtrtime1_m4 xtrtime1_m5 xtrtime1_m6 using "$tables/parentsmanatt.tex", append se nonotes style(tex) noobs b(%12.3f) se(%12.3f) ///
keep(treatindiv treattime) nolines collabels(none)  ///
cell(b(fmt(a3) star pvalue(ri)) se(fmt(a3) par) ri(fmt(a3) par([ ])))  ///
coeflabels(treatindiv "Treated" treattime "Treated $\times$ Time since treated") ///
		starlevels(* 0.10 ** 0.05 *** 0.01) nomtitles ///
		nonumbers  fragment  ///
		prehead( \\  ) ///
		posthead(\midrule \\) ///
		prefoot() ///
		postfoot()
		
esttab xtrrisk1_m1 xtrrisk1_m2 xtrrisk1_m3 xtrrisk1_m4 xtrrisk1_m5 xtrrisk1_m6 using "$tables/parentsmanatt.tex", append se nonotes style(tex) noobs scalar("sb Mean control men" "sg Mean control women" "o1 Observations") sfmt(%12.3f %12.3f %12.0f) b(%12.3f) se(%12.3f) ///
keep(treatindiv treatrisk) nolines collabels(none) ///
cell(b(fmt(a3) star pvalue(ri)) se(fmt(a3) par) ri(fmt(a3) par([ ])))  ///
coeflabels(treatindiv "Treated" treatrisk "Treated $\times$ environmental risk") ///
		starlevels(* 0.10 ** 0.05 *** 0.01) nomtitles ///
		nonumbers  fragment  ///
		prehead( \\ ) ///
		posthead(\midrule \\) ///
		prefoot( \\ \midrule) ///
		postfoot()	
	
}


* 6. Behaviors table

if 0{	
estimates clear


preserve
gen bn = 0 // generate another variable as a placement column to add joint p-value for now. 
la var bn "Joint test"


drop if spillsample == 1



/*

RI: average treatment


*/
randcmd ///
((treatindiv) eststo xtr1: xtreg b1 treatindiv post, fe nonest vce(cluster club_id)) ///
((treatindiv) eststo xtr2: xtreg b2 treatindiv post, fe nonest vce(cluster club_id)) ///
((treatindiv) eststo xtr3: xtreg b3 treatindiv post, fe nonest vce(cluster club_id)) ///
((treatindiv) eststo xtr4: xtreg b4 treatindiv post, fe nonest vce(cluster club_id)) ///
((treatindiv) eststo xtr5: xtreg b5 treatindiv post, fe nonest vce(cluster club_id)) ///
, treatvars(treatindividual) groupvar(club_id)   seed(1234) reps(1000) calc1(replace treatindiv = treatindividual*post)

matrix list e(RCoef) //  Randomization-c and -t p-values for individual coefficients.
matrix list e(RMult) //  Randomization-c and -t p-values for the Westfall-Young multiple testing 

// rand t p-value for each column
matrix rcoef = e(RCoef)

// WY p-value for last column 
matrix rmult = e(RMult)

matrix rivals = J(6,1,.)
forvalues i = 1/5 {
		matrix rivals[`i',1] = rcoef[`i',6]
}

matrix rivals[6,1] = rmult[6,6]
local wy = rmult[6,6]
mat colnames rivals = treatindiv 
mat rownames rivals = b1 b2 b3 b4 b5 bn

file open newfile  using "$tables/pwybehav.tex", write replace 
local wy: di %4.3f `wy'
file write newfile ("`wy'") 
file close newfile


foreach var in $envbehav bn {
  eststo xtr_`var': xtreg `var' treatindiv post, fe nonest vce(cluster club_id)
  estadd matrix ri = rivals["`var'",1...]
}

/*

RI: treatment and time


*/
	
randcmd ///
((treatindiv treattime) eststo xtr1: xtreg b1 treatindiv treattime post, fe nonest vce(cluster club_id)) ///
((treatindiv treattime) eststo xtr2: xtreg b2 treatindiv treattime post, fe nonest vce(cluster club_id)) ///
((treatindiv treattime) eststo xtr3: xtreg b3 treatindiv treattime post, fe nonest vce(cluster club_id)) ///
((treatindiv treattime) eststo xtr4: xtreg b4 treatindiv treattime post, fe nonest vce(cluster club_id)) ///
((treatindiv treattime) eststo xtr5: xtreg b5 treatindiv treattime post, fe nonest vce(cluster club_id)) ///
, treatvars(treatindividual) groupvar(club_id)  seed(1234) reps(1000) calc1(replace treatindiv = treatindividual*post) calc2(replace treattime = timesincetreatment*treatindividual*post)	
		

matrix list e(RCoef) //  Randomization-c and -t p-values for individual coefficients.
matrix list e(RMult) //  Randomization-c and -t p-values for the Westfall-Young multiple testing 

// rand t p-value for each column
matrix rcoef = e(RCoef)

// WY p-value for last column 
matrix rmult = e(RMult)

matrix rivals = J(6,2,.)
forvalues i = 1/5 {
		scalar odd = 2*`i' - 1   // odd placement across rows for treat
		local j = odd
		matrix rivals[`i',1] = rcoef[`j',6]
		scalar even = 2*`i'  // even placement across rows for treat*time
		local k = even
		matrix rivals[`i',2] = rcoef[`k',6]
}

matrix rivals[6,2] = rmult[6,6]

mat colnames rivals = treatindiv treattime
mat rownames rivals = b1 b2 b3 b4 b5 bn
matrix list rivals

foreach var in $envbehav bn  {
  eststo xtrtime1_`var': xtreg `var' treatindiv post treattime, fe nonest vce(cluster club_id)
  estadd matrix ri = rivals["`var'",1...]	
  
  } 
  
/*

RI:  treatment and gender


*/ 
	
randcmd ///
((treatindiv treatgender) eststo xtr1: xtreg b1 treatindiv treatgender post, fe nonest vce(cluster club_id)) ///
((treatindiv treatgender) eststo xtr2: xtreg b2 treatindiv treatgender post, fe nonest vce(cluster club_id)) ///
((treatindiv treatgender) eststo xtr3: xtreg b3 treatindiv treatgender post, fe nonest vce(cluster club_id)) ///
((treatindiv treatgender) eststo xtr4: xtreg b4 treatindiv treatgender post, fe nonest vce(cluster club_id)) ///
((treatindiv treatgender) eststo xtr5: xtreg b5 treatindiv treatgender post, fe nonest vce(cluster club_id)) ///
, treatvars(treatindividual) groupvar(club_id)    seed(1234) reps(1000) calc1(replace treatindiv = treatindividual*post) calc2(replace treatgender = dem_gender*treatindividual*post)	
		

matrix list e(RCoef) //  Randomization-c and -t p-values for individual coefficients.
matrix list e(RMult) //  Randomization-c and -t p-values for the Westfall-Young multiple testing 

// rand t p-value for each column
matrix rcoef = e(RCoef)

// WY p-value for last column 
matrix rmult = e(RMult)

matrix rivals = J(6,2,.)
forvalues i = 1/5 {
		scalar odd = 2*`i' - 1   // odd placement across rows for treat
		local j = odd
		matrix rivals[`i',1] = rcoef[`j',6]
		scalar even = 2*`i'  // even placement across rows for treat*time
		local k = even
		matrix rivals[`i',2] = rcoef[`k',6]
}

matrix rivals[6,2] = rmult[6,6]

mat colnames rivals = treatindiv treatgender
mat rownames rivals = b1 b2 b3 b4 b5 bn
matrix list rivals
  

foreach var in $envbehav bn {
  eststo xtrgen1_`var': xtreg `var' treatindiv post treatgender, fe nonest vce(cluster club_id)
  estadd matrix ri = rivals["`var'",1...]	
	}

	
/*

RI:  treatment and environmental risk


*/
randcmd ///
((treatindiv treatrisk) eststo xtr1: xtreg b1 treatindiv treatrisk post, fe nonest vce(cluster club_id)) ///
((treatindiv treatrisk) eststo xtr2: xtreg b2 treatindiv treatrisk post, fe nonest vce(cluster club_id)) ///
((treatindiv treatrisk) eststo xtr3: xtreg b3 treatindiv treatrisk post, fe nonest vce(cluster club_id)) ///
((treatindiv treatrisk) eststo xtr4: xtreg b4 treatindiv treatrisk post, fe nonest vce(cluster club_id)) ///
((treatindiv treatrisk) eststo xtr5: xtreg b5 treatindiv treatrisk post, fe nonest vce(cluster club_id)) ///
, treatvars(treatindividual) groupvar(club_id)   seed(1234) reps(1000) calc1(replace treatindiv = treatindividual*post) calc2(replace treatrisk = envrisk_zscore*treatindividual*post)	
		

matrix list e(RCoef) //  Randomization-c and -t p-values for individual coefficients.
matrix list e(RMult) //  Randomization-c and -t p-values for the Westfall-Young multiple testing 

// rand t p-value for each column
matrix rcoef = e(RCoef)

// WY p-value for last column 
matrix rmult = e(RMult)

matrix rivals = J(6,2,.)
forvalues i = 1/5 {
		scalar odd = 2*`i' - 1   // odd placement across rows for treat
		local j = odd
		matrix rivals[`i',1] = rcoef[`j',6]
		scalar even = 2*`i'  // even placement across rows for treat*time
		local k = even
		matrix rivals[`i',2] = rcoef[`k',6]
		
}

matrix rivals[6,2] = rmult[6,6]

mat colnames rivals = treatindiv treatrisk
mat rownames rivals = b1 b2 b3 b4 b5 bn

foreach var in $envbehav bn {
   
  eststo xtrrisk1_`var': xtreg `var' treatindiv post treatrisk envrisk_zscore, fe nonest vce(cluster club_id)
  estadd matrix ri = rivals["`var'",1...]
  sum `var' if dem_gender == 0 & treatment == 1 & post == 0
  local ymeanboys = round(r(mean),0.01)
  estadd scalar sb `ymeanboys':  xtrrisk1_`var'
  sum `var' if dem_gender == 1 & treatment == 1 & post == 0
  local ymeangirls = round(r(mean),0.01)
  estadd scalar sg `ymeangirls':  xtrrisk1_`var'
  sum `var'
  local obs = r(N) 	
  estadd scalar o1 `obs': xtrrisk1_`var'
}
 
restore




		
esttab xtr_b1 xtr_b2 xtr_b3 xtr_b4 xtr_b5 using "$tables/parentsbehav.tex", replace se nonotes noobs style(tex) ///
cell(b(fmt(a3) star pvalue(ri)) se(fmt(a3) par) ri(fmt(a3) par([ ])))  ///
keep(treatindiv) nolines ///
coeflabels(treatindiv "Treated") nolines collabels(none) ///
		starlevels(* 0.10 ** 0.05 *** 0.01) label nomtitles ///
		nonumbers  fragment  ///
		prehead( ) ///
		posthead(\\ \midrule \\) ///
		prefoot() ///
		postfoot()
		
esttab xtrgen1_b1 xtrgen1_b2 xtrgen1_b3 xtrgen1_b4 xtrgen1_b5 using "$tables/parentsbehav.tex", append se nonotes style(tex) noobs b(%12.3f) se(%12.3f) ///
keep(treatindiv treatgender) nolines  collabels(none) ///
cell(b(fmt(a3) star pvalue(ri)) se(fmt(a3) par) ri(fmt(a3) par([ ])))  ///
coeflabels(treatindiv "Treated" treatgender "Treated $\times$ Female") ///
		starlevels(* 0.10 ** 0.05 *** 0.01) nomtitles ///
		nonumbers  fragment  ///
		prehead( \\ ) ///
		posthead(\midrule \\) ///
		prefoot() ///
		postfoot()
		
esttab xtrtime1_b1 xtrtime1_b2 xtrtime1_b3 xtrtime1_b4 xtrtime1_b5 using "$tables/parentsbehav.tex", append se nonotes style(tex) noobs b(%12.3f) se(%12.3f) ///
keep(treatindiv treattime) nolines collabels(none)  ///
cell(b(fmt(a3) star pvalue(ri)) se(fmt(a3) par) ri(fmt(a3) par([ ])))  ///
coeflabels(treatindiv "Treated" treattime "Treated $\times$ Time since treated") ///
		starlevels(* 0.10 ** 0.05 *** 0.01) nomtitles ///
		nonumbers  fragment  ///
		prehead( \\  ) ///
		posthead(\midrule \\) ///
		prefoot() ///
		postfoot()
		
esttab xtrrisk1_b1 xtrrisk1_b2 xtrrisk1_b3 xtrrisk1_b4 xtrrisk1_b5 using "$tables/parentsbehav.tex", append se nonotes style(tex) noobs scalar("sb Mean control men" "sg Mean control women" "o1 Observations") sfmt(%12.3f %12.3f %12.0f) b(%12.3f) se(%12.3f) ///
keep(treatindiv treatrisk) nolines collabels(none) ///
cell(b(fmt(a3) star pvalue(ri)) se(fmt(a3) par) ri(fmt(a3) par([ ])))  ///
coeflabels(treatindiv "Treated" treatrisk "Treated $\times$ environmental risk") ///
		starlevels(* 0.10 ** 0.05 *** 0.01) nomtitles ///
		nonumbers  fragment  ///
		prehead( \\ ) ///
		posthead(\midrule \\) ///
		prefoot( \\ \midrule) ///
		postfoot()

	
		
*Testing estimation for list experiment

gen countlitter1 = behavior_10_1_1
replace countlitter1 = behavior_10_2_1 if countlitter1 == .
gen litterlist1 = behavior_10_1_1 ~=.

gen countlitter2 = behavior_10_1_2
replace countlitter2 = behavior_10_2_2 if countlitter2 == .
gen litterlist2 = behavior_10_2_2 ~=.

reg countlitter1 i.treatindiv##i.litterlist1 dem_gender dem_age simpleindex if post == 1, robust
reg countlitter2 i.treatindiv##i.litterlist2 dem_gender dem_age simpleindex if post == 1, robust
		
		

}	

* 7. Summary aggregates table 

if 0{
estimates clear


preserve
gen yn = 0 // generate another variable as a placement column to add joint p-value for now. 
la var yn "Joint test"


drop if spillsample == 1


/*

RI: average treatment
 

*/
randcmd ///
((treatindiv) eststo xtr1: xtreg y5 treatindiv post, fe nonest vce(cluster club_id)) ///
((treatindiv) eststo xtr2: xtreg a7 treatindiv post, fe nonest vce(cluster club_id)) ///
((treatindiv) eststo xtr3: xtreg m3 treatindiv post, fe nonest vce(cluster club_id)) ///
((treatindiv) eststo xtr4: xtreg b5 treatindiv post, fe nonest vce(cluster club_id)) ///
, treatvars(treatindividual) groupvar(club_id)   seed(1234) reps(1000) calc1(replace treatindiv = treatindividual*post)

matrix list e(RCoef) //  Randomization-c and -t p-values for individual coefficients.
matrix list e(RMult) //  Randomization-c and -t p-values for the Westfall-Young multiple testing 

// rand t p-value for each column
matrix rcoef = e(RCoef)

// WY p-value for last column 
matrix rmult = e(RMult)

matrix rivals = J(5,1,.)
forvalues i = 1/4 {
		matrix rivals[`i',1] = rcoef[`i',6]
}

matrix rivals[5,1] = rmult[5,6]
local wy = rmult[5,6]
mat colnames rivals = treatindiv 
mat rownames rivals = y5 a7 m3 b5 yn

file open newfile  using "$tables/pwysum.tex", write replace 
local wy: di %4.3f `wy'
file write newfile ("`wy'")  
file close newfile


foreach var in $summarytable yn {
  eststo xtr_`var': xtreg `var' treatindiv post, fe nonest vce(cluster club_id)
   estadd matrix ri = rivals["`var'",1...]
}

/*

RI: treatment and time


*/
	
randcmd ///
((treatindiv treattime) eststo xtr1: xtreg y5 treatindiv treattime post, fe nonest vce(cluster club_id)) ///
((treatindiv treattime) eststo xtr2: xtreg a7 treatindiv treattime post, fe nonest vce(cluster club_id)) ///
((treatindiv treattime) eststo xtr3: xtreg m3 treatindiv treattime post, fe nonest vce(cluster club_id)) ///
((treatindiv treattime) eststo xtr4: xtreg b5 treatindiv treattime post, fe nonest vce(cluster club_id)) ///
, treatvars(treatindividual) groupvar(club_id)  seed(1234) reps(1000) calc1(replace treatindiv = treatindividual*post) calc2(replace treattime = timesincetreatment*treatindividual*post)	
		

matrix list e(RCoef) //  Randomization-c and -t p-values for individual coefficients.
matrix list e(RMult) //  Randomization-c and -t p-values for the Westfall-Young multiple testing 

// rand t p-value for each column
matrix rcoef = e(RCoef)

// WY p-value for last column 
matrix rmult = e(RMult)

matrix rivals = J(5,2,.)
forvalues i = 1/4 {
		scalar odd = 2*`i' - 1   // odd placement across rows for treat
		local j = odd
		matrix rivals[`i',1] = rcoef[`j',6]
		scalar even = 2*`i'  // even placement across rows for treat*time
		local k = even
		matrix rivals[`i',2] = rcoef[`k',6]
}

matrix rivals[5,2] = rmult[5,6]

mat colnames rivals = treatindiv treattime
mat rownames rivals =y5 a7 m3 b5 yn
matrix list rivals

foreach var in $summarytable yn  {
  eststo xtrtime1_`var': xtreg `var' treatindiv post treattime, fe nonest vce(cluster club_id)
  estadd matrix ri = rivals["`var'",1...]	
  
  } 
  
/*

RI:  treatment and gender


*/ 
	
randcmd ///
((treatindiv treatgender) eststo xtr1: xtreg y5 treatindiv treatgender post, fe nonest vce(cluster club_id)) ///
((treatindiv treatgender) eststo xtr2: xtreg a7 treatindiv treatgender post, fe nonest vce(cluster club_id)) ///
((treatindiv treatgender) eststo xtr3: xtreg m3 treatindiv treatgender post, fe nonest vce(cluster club_id)) ///
((treatindiv treatgender) eststo xtr4: xtreg b5 treatindiv treatgender post, fe nonest vce(cluster club_id)) ///
, treatvars(treatindividual) groupvar(club_id)  seed(1234) reps(1000) calc1(replace treatindiv = treatindividual*post) calc2(replace treatgender = dem_gender*treatindividual*post)	
		

matrix list e(RCoef) //  Randomization-c and -t p-values for individual coefficients.
matrix list e(RMult) //  Randomization-c and -t p-values for the Westfall-Young multiple testing 

// rand t p-value for each column
matrix rcoef = e(RCoef)

// WY p-value for last column 
matrix rmult = e(RMult)

matrix rivals = J(5,2,.)
forvalues i = 1/4 {
		scalar odd = 2*`i' - 1   // odd placement across rows for treat
		local j = odd
		matrix rivals[`i',1] = rcoef[`j',6]
		scalar even = 2*`i'  // even placement across rows for treat*time
		local k = even
		matrix rivals[`i',2] = rcoef[`k',6]
}

matrix rivals[5,2] = rmult[5,6]

mat colnames rivals = treatindiv treatgender
mat rownames rivals = y5 a7 m3 b5 yn
matrix list rivals
  

foreach var in $summarytable yn {
  eststo xtrgen1_`var': xtreg `var' treatindiv post treatgender, fe nonest vce(cluster club_id)
  estadd matrix ri = rivals["`var'",1...]	
	}

	
/*

RI:  treatment and environmental risk


*/
randcmd ///
((treatindiv treatrisk) eststo xtr1: xtreg y5 treatindiv treatrisk post, fe nonest vce(cluster club_id)) ///
((treatindiv treatrisk) eststo xtr2: xtreg a7 treatindiv treatrisk post, fe nonest vce(cluster club_id)) ///
((treatindiv treatrisk) eststo xtr3: xtreg m3 treatindiv treatrisk post, fe nonest vce(cluster club_id)) ///
((treatindiv treatrisk) eststo xtr4: xtreg b5 treatindiv treatrisk post, fe nonest vce(cluster club_id)) ///
, treatvars(treatindividual) groupvar(club_id)  seed(1234) reps(1000) calc1(replace treatindiv = treatindividual*post) calc2(replace treatrisk = envrisk_zscore*treatindividual*post)	
		

matrix list e(RCoef) //  Randomization-c and -t p-values for individual coefficients.
matrix list e(RMult) //  Randomization-c and -t p-values for the Westfall-Young multiple testing 

// rand t p-value for each column
matrix rcoef = e(RCoef)

// WY p-value for last column 
matrix rmult = e(RMult)

matrix rivals = J(5,2,.)
forvalues i = 1/4 {
		scalar odd = 2*`i' - 1   // odd placement across rows for treat
		local j = odd
		matrix rivals[`i',1] = rcoef[`j',6]
		scalar even = 2*`i'  // even placement across rows for treat*time
		local k = even
		matrix rivals[`i',2] = rcoef[`k',6]
		
}

matrix rivals[5,2] = rmult[5,6]

mat colnames rivals = treatindiv treatrisk
mat rownames rivals = y5 a7 m3 b5 yn

foreach var in $summarytable yn {
   
  eststo xtrrisk1_`var': xtreg `var' treatindiv post treatrisk envrisk_zscore, fe nonest vce(cluster club_id)
  estadd matrix ri = rivals["`var'",1...]
  sum `var' if  dem_gender == 0 & treatment == 1 & post == 0
  local ymeanboys = round(r(mean),0.01)
  estadd scalar sb `ymeanboys':  xtrrisk1_`var'
  sum `var' if dem_gender == 1 & treatment == 1 & post == 0
  local ymeangirls = round(r(mean),0.01)
  estadd scalar sg `ymeangirls':  xtrrisk1_`var'
  sum `var'
  local obs = r(N) 	
  estadd scalar o1 `obs': xtrrisk1_`var'
}
 
restore




		
esttab xtr_m3 xtr_y5 xtr_a7  xtr_b5 using "$tables/summaryparents.tex", replace se nonotes noobs style(tex) ///
cell(b(fmt(a3) star pvalue(ri)) se(fmt(a3) par) ri(fmt(a3) par([ ])))  ///
keep(treatindiv) nolines ///
coeflabels(treatindiv "Treated") nolines collabels(none) ///
		starlevels(* 0.10 ** 0.05 *** 0.01) label nomtitles ///
		nonumbers  fragment  ///
		prehead( ) ///
		posthead(\\ \midrule \\) ///
		prefoot() ///
		postfoot()
		
esttab xtrgen1_m3 xtrgen1_y5 xtrgen1_a7  xtrgen1_b5 using "$tables/summaryparents.tex", append se nonotes style(tex) noobs b(%12.3f) se(%12.3f) ///
keep(treatindiv treatgender) nolines  collabels(none) ///
cell(b(fmt(a3) star pvalue(ri)) se(fmt(a3) par) ri(fmt(a3) par([ ])))  ///
coeflabels(treatindiv "Treated" treatgender "Treated $\times$ Female") ///
		starlevels(* 0.10 ** 0.05 *** 0.01) nomtitles ///
		nonumbers  fragment  ///
		prehead( \\ ) ///
		posthead(\midrule \\) ///
		prefoot() ///
		postfoot()
		
esttab xtrtime1_m3 xtrtime1_y5 xtrtime1_a7  xtrtime1_b5 using "$tables/summaryparents.tex", append se nonotes style(tex) noobs b(%12.3f) se(%12.3f) ///
keep(treatindiv treattime) nolines collabels(none)  ///
cell(b(fmt(a3) star pvalue(ri)) se(fmt(a3) par) ri(fmt(a3) par([ ])))  ///
coeflabels(treatindiv "Treated" treattime "Treated $\times$ Time since treated") ///
		starlevels(* 0.10 ** 0.05 *** 0.01) nomtitles ///
		nonumbers  fragment  ///
		prehead( \\  ) ///
		posthead(\midrule \\) ///
		prefoot() ///
		postfoot()
		
esttab xtrrisk1_m3 xtrrisk1_y5 xtrrisk1_a7  xtrrisk1_b5 using "$tables/summaryparents.tex", append se nonotes style(tex) noobs ///
 scalar("sb Mean control men" "sg Mean control women" "o1 Observations") sfmt(%12.3f %12.3f %12.0f) b(%12.3f) se(%12.3f) ///
keep(treatindiv treatrisk) nolines collabels(none) ///
cell(b(fmt(a3) star pvalue(ri)) se(fmt(a3) par) ri(fmt(a3) par([ ])))  ///
coeflabels(treatindiv "Treated" treatrisk "Treated $\times$ environmental risk") ///
		starlevels(* 0.10 ** 0.05 *** 0.01) nomtitles ///
		nonumbers  fragment  ///
		prehead( \\ ) ///
		posthead(\midrule \\) ///
		prefoot( \\ \midrule) ///
		postfoot()
	
	
	
}

* 8. Summary figures

if 1{
	
global summarytable m3 y5 a7 b5 
drop if spillsample == 1

foreach var in $summarytable  {
	
	xtreg `var' treatindiv post, fe nonest vce(cluster club_id)
	estimates store b`var'
}	

	
	coefplot (bm3,asequation("Rank mangrove project") \ by5, asequation("Knowledge index") \ ba7, asequation("Attitudes index") \ bb5, asequation("Behavior index")) , msize(vlarge) xline(0) ciopts(recast(rcap)) ///  
	xtitle("Treatment effect") keep(treatindiv)  aseq swapnames 
	graph export "${figures}\parentsavgmfx.png", replace	
	
mat A = J(4,3,.)				// matrix to store treatment results for male
mat B = J(4,3,.)				// matrix to store treatment results for female
	

local n = 1
foreach var in $summarytable  {

 eststo t`var':  xtreg `var' i.treatindiv##i.dem_gender post, fe nonest vce(cluster club_id)
	mat A[`n',1] = r(table)[1,2]		// coefficient of 1.treat
	mat A[`n',2] = r(table)[5,2]		// lower CI boundary of 1.treat
	mat A[`n',3] = r(table)[6,2]		// upper CI boundary of 1.treat
	lincom 	1.treatindiv +1.treatindiv#1.dem_gender	
	mat B[`n',1] = r(estimate)		// coefficient of 1.treat+1.treat#1.female
	mat B[`n',2] = r(lb)			// lower CI boundary 
	mat B[`n',3] = r(ub)			// upper CI boundary
	local n = `n'+1	  

}

  coefplot (mat(A[.,1]), ci((A[,2] A[,3])) msize(vlarge) pstyle(p3) ciopts(recast(rcap))) (mat(B[.,1]), ci((B[,2] B[,3])) msize(vlarge) pstyle(p2) ciopts(recast(rcap))),  xline(0)		///
  	coeflab(r1="Rank mangrove project" r2="Knowledge index" r3="Attitudes index" r4="Behavior index") 				///
	plotlab("Male" "Female")
graph export "${figures}\parentsgender.png", replace
}
